package com.example.h1nson.realphotography.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.config.ConfigURL;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.util.AsyncImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by H1nson on 2016/6/15.
 */
public class PushAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List list;
    public List<ImageView> listView;
    private int presentPosition;
    public PushAdapter(Context context, List<Map> list){
        this.list = list;
        this.listView = new ArrayList<ImageView>();
        this.inflater = LayoutInflater.from(context);
        this.presentPosition=0;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.listview_push_ltem, null);
        }
        Map map = (Map) list.get(position);
        String URLStr = (String) map.get("photoUrl");
        String AddressStr = (String) map.get("photoAddress");
        double Distance = (Double) map.get("photoDistance");
            ImageView imageView = (ImageView) convertView.findViewById(R.id.listview_push_photo);
            TextView textViewAddress = (TextView) convertView.findViewById(R.id.listview_push_text1);
            TextView textViewDistance = (TextView) convertView.findViewById(R.id.listview_push_text2);
            WindowManager wm = (WindowManager) inflater.getContext()
                    .getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            ViewGroup.LayoutParams ps = imageView.getLayoutParams();
            ps.height = (int)width*9/10;
            ps.width = (int)width*9/10;
            imageView.setLayoutParams(ps);
            listView.add(imageView);
            System.out.println("position"+position);
            setPhoto(URLStr,position);
            textViewAddress.setText(AddressStr);
            textViewDistance.setText("("+Distance+"km)");

        return convertView;
    }
    private void setPhoto(String URL,final int position){
        System.out.println("setPhoto");
        AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
        String imageUrl = ConfigURL.photoURL+URL;
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new AsyncImageLoader.ImageCallback() {
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                if (imageDrawable == null) {
                    //imageView.setImageResource(R.drawable.icon);
                    //imageDrawable = getResources().getDrawable(R.drawable.user_image);
                }
                ImageView imageViewPhoto = listView.get(position);
                imageViewPhoto.setImageDrawable(imageDrawable);
            }
        });
    }
}
